package com.itheima.app.controller;

import com.easemob.im.server.api.token.allocate.TokenRequest;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.PublicKey;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    @PostMapping("/user/save")
    public ResponseEntity save(@RequestBody User user) {
        return userManager.save(user);
    }

    @GetMapping("/user/findByPhone")
    public ResponseEntity findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    @PostMapping("/user/login")
    public void smsSend(@RequestBody Map<String, String> param) {
        //1 取出手机号
        String phone = param.get("phone");
        //2 调用manager
        userManager.smsSend(phone);
    }

    //验证码校验登录
    @PostMapping("/user/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> param) {
        //1.接受参数
        String phone = param.get("phone");
        String verificationCode = param.get("verificationCode");
        //2.调用manager
        return userManager.loginVerification(phone, verificationCode);
    }

    //保存用户基本信息
    @PostMapping("/user/loginReginfo")
    public void loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        userManager.loginReginfo(userInfo, token);
    }


    //保存头像信息
    @PostMapping({"/user/loginReginfo/head","/users/header"})

    public ResponseEntity loginReginfoHead(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {

       return userManager.loginReginfoHead(headPhoto, token);
    }

    //查询用户信息
    @GetMapping("/users")
    public ResponseEntity findUserInfoVo(@RequestParam(value = "userID", required = false) Long userId, @RequestHeader("Authorization") String token) {
        //判断
        if (userId != null) {
            return userManager.findUserInfoVo(userId);
        } else {
            User user = userManager.findUserByToken(token);
            return userManager.findUserInfoVo(user.getId());
        }
    }

    //更新用户信息
    @PutMapping("/users")
    public void putUserInfo(@RequestBody UserInfo userInfo) {
        //调用manager
        userManager.putUserInfo(userInfo);

    }




    //修改手机号
    @PostMapping("/users/phone/sendVerificationCode")
    public void updatePhonesmsSend() {
        userManager.smsSend(UserHolder.get().getMobile());
    }
    //校验
    @PostMapping("/users/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String, String> parm) {

        String verificationCode = parm.get("verificationCode");


        return userManager.verificationCode(verificationCode);
    }
    //修改手机号
    @PostMapping("/users/phone")
    public ResponseEntity updatephone(@RequestBody Map<String, String> parm, @RequestHeader("Authorization") String token) {
        String phone = parm.get("phone");
        return userManager.updatephone(phone,token);
    }



}
